moddbfandomcom-20200213-history
HGE:Tutorials:Particle Effects
=Particle systems= Particle effects in HGE use the hgeParticleSystem helper class. To create particle effects, you need to first use the Particle System Editor which is available in the hge\tools\particleed directory, assuming hge is the directory where you installed HGE. Run particleed.exe to start the editor. Create your desired particle in the editor, adjusting the parameters as necessary. For a more complete guide to the particle system editor, see HGE Particle System Editor The particle editor automatically saves the particles you created. Obtain this saved particle by looking in the particleed directory and retrieving a file named particleX.psi, where X is the number of the particle you modified (this number is shown beneath the "Presets & Stuff" section in the lower-right). This .psi file is the particle description file, which will be referred to later on. Files:HGE_warn.gif Note For this tutorial, be sure that you create a particle with a limited lifetime, not a continuous lifetime particle. In other words, the particle must disappear on its own after a specified duration. The "continuous" box in the upper-left corner should not be checked. =Defining particles in the resource manager= Here is a sample particle description file you can use for this tutorial, or you can use your own: Media:HGE_particle.psi Make sure to place it in the current directory and name it particle.psi To define a particle effect, give it a name and specify the following parameters: :filename: The name of the particle description file (.psi) for the particle. :sprite: The name of the sprite it uses. :fps: The speed of the particle in frames per second. Files:HGE_warn.gif Caution The .psi file does NOT include the sprite that is associated with the particle! You must define a sprite separately and then specify that you want to use that sprite, by using the sprite parameter. Add the following to the resource.res file: Particle myParticle { filename=particle.psi sprite=playerSprite fps=50.0 } For simplicity reasons, we'll just use the player sprite as a particle. =Displaying particles= First, we need the particle's info structure. This contains the parameters we defined when we created the particle (speed, lifetime, color, etc.). Next we need to create a particle manager instance in order to display the particles. hgeParticleManager is an HGE helper class that takes care of all your particle systems and automates their creation, updating and removing. This particle manager can handle up to 100 particles at once, any new particles won't be created if there are more than 100. Add the following at the top of the program: #include hgeParticleSystemInfo myParticle; hgeParticleManager *particleManager; In WinMain, below the other initializations, add: myParticle = myRes->GetParticleSystem("myParticle")->info; particleManager= new hgeParticleManager(); This retrieves the particle's info from the resource script file and creates a new instance of a particle manager. Make sure to delete the particle manager before WinMain returns: delete particleManager; What we will do for this tutorial is simple: Spawn a particle wherever the user clicks (in addition to the sound effect being played). Go to the FrameFunc and make sure the statement where we played the sound now looks like this: //when left mouse is clicked, play sound effect and spawn particle if(hge->Input_GetKey() HGEK_LBUTTON){ chan1 = hge->Effect_Play(mySound); } Below the chan1 statement, add the following: particleManager->SpawnPS(&myParticle, mouseX, mouseY); This will spawn a particle effect when the user clicks the left mouse button. The SpawnPS() member function takes in 3 parameters: the address of the particle's info, and the X and Y coordinates of the desired location. Two more statements must be added in order for the particle to be displayed properly. The particle must be properly updated, just like we did for animations. It also has to be rendered, between the Gfx_BeginScene() and Gfx_EndScene() pairs. Add the Update() function below the animation's update function, in FrameFunc: particleManager->Update(dt); //update all particles Add the Render() function below all the other Render() statements, in FrameFunc: particleManager->Render(); //render all particles Run the program, and whenever you click, particles will be spawned! center You can download the source file and resource script file for this tutorial here: Tutorial 7 source, Resource script This concludes the HGE tutorial. You may visit the official site, HGE, for in-depth information on specific functions. The next sections will discuss some miscellaneous items in HGE, and the final section will demonstrate how to build a simple game with HGE. Next section: Random Numbers Category:C and HGE Category:HGE